﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Arroguella.Utilities
{
    /// <summary>
    /// Defines an object responsible for maintaining a collection of strings, 
    /// and being able to choose one at random.
    /// </summary>
    public class RandomStringChooser : RandomChooser<string>
    {
        #region Constructors

        public RandomStringChooser(ICollection<string> collection)
            : base(collection)
        {
        }

        /// <summary>
        /// Creates a RandomStringChooser based on an XML file and an XML node
        /// </summary>
        /// <param name="xmlFile">The XML file</param>
        /// <param name="rootNode">The node that contains the values which are to be put into the RandomStringChooser</param>
        public static RandomStringChooser RandomStringChooserFromXml(string xmlFile, string rootNode)
        {
            var doc = XElement.Load(xmlFile);
            var root = doc.Element(rootNode);
            if (root != null)
            {
                var collection = root.Value.Split(',').ToList();
                return new RandomStringChooser(collection);
            }

            return new RandomStringChooser(new List<string>());
        }

        #endregion
    }
}
